<?php
/**
 * Create by Phpstorm
 * User: Peco
 * Date: 2019/12/19
 * Time: 16:08
 */

namespace App\Repositories;

use App\Models\AuctionGoodsInfo;
use App\Models\Goods;
use App\Models\UserAuctionRecord;

class AuctionRepository extends BaseRepositories
{
    /**
     * @var UserAuctionRecord 注入的模型
     */
    protected $userAuctionRecord;

    /**
     * @var AuctionGoodsInfo 注入的模型
     */
    protected $auctionGoodsInfo;

    /**
     * @var Goods 注入的Goods model
     */
    protected $goods;

    /**
     * AuctionRepository constructor.
     * @param UserAuctionRecord $userAuctionRecord
     */
    public function __construct(UserAuctionRecord $userAuctionRecord,
                                AuctionGoodsInfo $auctionGoodsInfo,
                                Goods $goods)
    {
        $this->userAuctionRecord = $userAuctionRecord;
        $this->auctionGoodsInfo = $auctionGoodsInfo;
        $this->goods = $goods;
    }

    /**
     * @param array $data
     * @param int $limit
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function auctionRecordPageRepository(array $data, int $limit){
        return $this->userAuctionRecord
            ->with(['user', 'auctionGoodsInfo', 'goods'])
            ->when($data['name'], function ($q)use($data){
                $q->whereHas('user', function ($q)use($data){
                    $q->where('nickname', 'like', '%'.$data['name'].'%')
                        ->orWhere('realname', 'like', '%'.$data['name'].'%');
                });
            })
            ->when($data['goodsName'], function ($q)use($data){
                $q->whereHas('goods', function ($q)use($data){
                    $q->where('goodsName', 'like', '%'.$data['goodsName'].'%');
                });
            })
            ->when($data['goodsAuctionInfoStatusOptions'], function ($q)use($data){
                $q->whereHas('auctionGoodsInfo', function ($q)use($data){
                    $q->where('status', $data['goodsAuctionInfoStatusOptions']);
                });
            })
            ->when(!is_null($data['auctionStatus']), function ($q)use($data){
                $q->where('auctionStatus', $data['auctionStatus']);
            })
            ->orderByDesc('updated_at')
            ->paginate($limit);
    }

    /***
     * @param array $data
     * @param int $limit
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function auctionGoodsPageRepository(array $data, int $limit){
        return $this->goods
            ->with(['category'=>function($q){
                $q->select('name', 'categoryId');
            }, 'auctionGoods'])
            ->whereHas('auctionGoods')
            ->when($data['status'], function ($q)use($data){
                $q->whereHas('auctionGoods', function ($q)use($data){
                    $q->where('status', $data['status']);
                });
            })
            ->when($data['goodsName'], function ($q)use($data){
                $q->where('goodsName', 'like', '%'.$data['goodsName'].'%');
            })
            ->when($data['goodsType'], function ($q)use($data){
                $q->where('goodsType', $data['goodsType']);
            })
            ->when($data['categoryId'], function ($q)use($data){
                $q->where('categoryId', $data['categoryId']);
            })
            ->paginate($limit);
    }

}
